package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* @author 听风寂
* @date 2020/1/6
* @ClassName SqlSessionUtil
* @Description 类描述
*/
public class SqlSessionUtil {
	/**
	 * 底层对应的是Connection连接池
	 */
	private static SqlSessionFactory ssf;
	static {
		InputStream in;
		try {
			// 从classpath位置加载文件，可以使用相对路径如:cn/hncu/mybatis-config.xml
			in = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
			ssf=ssfb.build(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	public static SqlSessionFactory getSsf() {
		return ssf;
	}
 
	public static SqlSession getSqlSession() {
		return ssf.openSession();
	}
}
